<template>
  <div class="container">
    <div>
      <el-carousel>
        <el-carousel-item v-for="(item, index) in banners" :key="index">
          <div class="banner-image" v-bind:style="{ backgroundImage: `url( ${item.url} )` }"></div>
        </el-carousel-item>
      </el-carousel>
    </div>

    <div class="banner-content">
      <div class="search-bar">

        <!-- tab栏 -->
        <el-row type="flex" class="search-tab">
          <!--
            需求：导航高亮效果，点谁谁高亮
            分析：点击谁就给谁添加active类名即可实现
          -->
          <span v-for="(item, index) in options" :key="index" 
          :class="{ active: index == currentOption }"
            @click="handleOption(index)">
            <i>{{ item.name }}</i>
          </span>
        </el-row>

        <!-- 输入框 -->
        <el-row type="flex" align="middle" class="search-input">
          <input :placeholder="options[currentOption].placeholder" v-model="searchValue" @keyup.enter="handleSearch" />
          <i class="el-icon-search" @click="handleSearch"></i>
        </el-row>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'IndexPage',
  data() {
    return {
      banners: [],    // 轮播图数据
      options: [      // 搜索框tab选项
        {
          name: "攻略",
          placeholder: "搜索城市",
          pageUrl: "/post?city="
        },
        {
          name: "酒店",
          placeholder: "请输入城市搜索酒店",
          pageUrl: "/hotel?city="
        },
        {
          name: "机票",
          placeholder: "请输入出发地",
          pageUrl: "/air"
        }
      ],
      searchValue: "",    // 搜索框的值
      currentOption: 0,   // 当前选中的选项     
    }
  },
  //mounted是内置函数，在页面打开时执行，自动，叫钩子函数
  mounted() {
    //调用methods终函数的时候必须加this
    this.getBanners()
  },
  methods: {
    getBanners() {
      //axios的使用
      // console.log(this.$axios)
      this.$axios({
        url: `/scenics/banners`,
        method:'get'
      }).then(res => {
        console.log('后端返回的数据', res)
        this.banners = res.data.data
        for (let i = 0; i < this.banners.length; i++) {
          this.banners[i].url = this.$axios.defaults.baseURL + this.banners[i].url
        }
      })
    },
    handleOption(index){
      this.currentOption=index
      console.log(this.currentOption)
      let item = this.options[this.currentOption];
      if(item.name=='机票')
        this.$router.push(item.pageUrl );
    },
    handleSearch(){
      console.log(this.currentOption)
      const item = this.options[this.currentOption];
    // 跳转时候给对应的页面url加上搜索内容参数
      this.$router.push(item.pageUrl + this.searchValue);
    },
  }
}
</script>

<style scoped lang="less">
@colorRed: red;

.container {
  min-width: 1000px;
  margin: 0 auto;
  position: relative;
  background: skyblue;

  /deep/ .el-carousel__container {
    height: 700px;
  }

  .banner-image {
    width: 100%;
    height: 100%;
    background-size: contain contain;
  }

  ul {
    li {
      font-size: 20px;
      color: white;

      //&相当于列标签，相当于下面那种语法
      &:nth-child(2) {
        color: @colorRed;
      }
    }

    // li:nth-child(2){
    //   color: red;
    // }
  }

  .banner-content {
    z-index: 9;
    width: 1000px;
    position: absolute;
    left: 50%;
    top: 8%;
    margin-left: -500px;
    border-top: 1px transparent solid;

    .search-bar {
      width: 552px;
      margin: 0 auto;
    }
    .search-tab {
      .active {
        i {
          color: #333;
        }

        &::after {
          background: #eee;
        }
      }

      span {
        width: 82px;
        height: 36px;
        display: block;
        position: relative;
        margin-right: 8px;
        cursor: pointer;

        i {
          position: absolute;
          z-index: 2;
          display: block;
          width: 100%;
          height: 100%;
          line-height: 30px;
          text-align: center;
          color: #fff;
        }

        &:after {
          position: absolute;
          left: 0;
          top: 0;
          display: block;
          content: "";
          width: 100%;
          height: 100%;
          border: 1px rgba(255, 255, 255, .2) solid;
          border-bottom: none;
          transform: scale(1.1, 1.3) perspective(.7em) rotateX(2.2deg);
          transform-origin: bottom left;
          background: rgba(0, 0, 0, .5);
          border-radius: 1px 2px 0 0;
          box-sizing: border-box;
        }
      }
    }

    .search-input {
      width: 550px;
      height: 46px;
      background: #fff;
      border-radius: 0 4px 4px 4px;
      border: 1px rgba(255, 255, 255, .2) solid;
      border-top: none;
      box-sizing: unset;

      input {
        flex: 1;
        height: 20px;
        padding: 13px 15px;
        outline: none;
        border: 0;
        font-size: 16px;
      }

      .el-icon-search {
        cursor: pointer;
        font-size: 22px;
        padding: 0 10px;
        font-weight: bold;
      }
    }
  }
}
</style>